<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Procedure properties</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="Application-and-Arguments-Lists.xhtml" title="Application and Arguments Lists"/>
    <link rel="next" href="Generic-procedures.xhtml" title="Generic (dynamically overloaded) procedures"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Procedure properties" epub:type="subchapter" id="Procedure-properties">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Procedure properties</h2>
          </div>
        </div>
      </div>
      <p>You can associate arbitrary <em class="firstterm">properties</em> with any procedure.
Each property is a (<em class="replaceable"><code>key</code></em>, <em class="replaceable"><code>value</code></em>)-pair.  Usually the
<em class="replaceable"><code>key</code></em> is a symbol, but it can be any object.
</p>
      <p>The system uses certain internal properties:
<code class="literal">'name</code> refers to the name used when a procedure is printed;
<code class="literal">'emacs-interactive</code> is used to implement Emacs <code class="literal">interactive</code>
specification;
<code class="literal">'setter</code> is used to associate a <code class="literal">setter</code> procedure.
</p>
      <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877396704" class="indexterm"/> <code class="function">procedure-property</code> <em class="replaceable"><code>proc</code></em> <em class="replaceable"><code>key</code></em> [<em class="replaceable"><code>default</code></em>]</p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>Get the property value corresponding to the given <em class="replaceable"><code>key</code></em>.
If <em class="replaceable"><code>proc</code></em> has no property with the given <em class="replaceable"><code>key</code></em>,
return <em class="replaceable"><code>default</code></em> (which defaults to <code class="literal">#f</code>) instead.
</p>
        </blockquote>
      </div>
      <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877390208" class="indexterm"/> <code class="function">set-procedure-property!</code> <em class="replaceable"><code>proc</code></em> <em class="replaceable"><code>key</code></em> <em class="replaceable"><code>value</code></em></p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>Associate the given <em class="replaceable"><code>value</code></em> with the <em class="replaceable"><code>key</code></em> property of <em class="replaceable"><code>proc</code></em>.
</p>
        </blockquote>
      </div>
      <p>To change the print name of the standard <code class="literal">+</code> procedure (probably
not a good idea!), you could do:
</p>
      <pre class="screen">(set-procedure-property! + 'name 'PLUS)
</pre>
      <p>Note this <span class="emphasis"><em>only</em></span> changes the name property used for printing:
</p>
      <pre class="screen">+ ⇒ #&lt;procedure PLUS&gt;
(+ 2 3) ⇒ 5
(PLUS 3 4) ⇒ ERROR
</pre>
      <p>As a matter of style, it is cleaner to use the <code class="literal">define-procedure</code>
form, as it is a more declarative interface.
</p>
      <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667877381456" class="indexterm"/> <code class="function">define-procedure</code> <em class="replaceable"><code>name</code></em> [<em class="replaceable"><code>propname:</code></em> <em class="replaceable"><code>propvalue</code></em>] <em class="replaceable"><code>...</code></em> <em class="replaceable"><code>method</code></em> <em class="replaceable"><code>...</code></em></p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>Defines <em class="replaceable"><code>name</code></em> as a compound procedure consisting of the
specified <em class="replaceable"><code>method</code></em>s, with the associated properties.
Applying <em class="replaceable"><code>name</code></em> select the "best" <em class="replaceable"><code>method</code></em>, and applies that.
See the following section on generic procedures.
</p>
          <p>For example, the standard <code class="literal">vector-ref</code> procedure specifies
one method, as well as the <code class="literal">setter</code> property:
</p>
          <pre class="screen">(define-procedure vector-ref
  setter: vector-set!
  (lambda (vector::vector k ::int)
    (invoke vector 'get k)))
</pre>
        </blockquote>
      </div>
      <p>You can also specify properties in the lambda body:
</p>
      <pre class="screen">(define (vector-ref vector::vector k ::int)
    setter: vector-set!
    (invoke vector 'get k))
</pre>
      <section class="sect2" title="Standard properties" epub:type="division" id="idm139667877371584">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Standard properties</h3>
            </div>
          </div>
        </div>
        <div class="variablelist" epub:type="list">
          <dl class="variablelist">
            <dt class="term"><code class="literal">name</code>
</dt>
            <dd>
              <p>The name of a procedure (as a symbol), which is used
when the procedure is printed.
</p>
            </dd>
            <dt class="term"><code class="literal">setter</code>
</dt>
            <dd>
              <p>Set the setter procedure associated with the procedure.
</p>
            </dd>
            <dt class="term"><code class="literal">validate-apply</code>
</dt>
            <dt class="term"><code class="literal">validate-xapply</code>
</dt>
            <dd>
              <p>Used during the validation phase of the compiler.
</p>
            </dd>
            <dt class="term"><code class="literal">compile-apply</code>
</dt>
            <dd>
              <p>Used during the bytecode-generation phase of the compiler:
If we see a call to a known function with this property,
we can emit custom bytecode for the call.
</p>
            </dd>
          </dl>
        </div>
      </section>
    </section>
    <footer>
      <div class="navfooter">
        <ul>
          <li>
            <b class="toc">
              <a href="Procedure-properties.xhtml#idm139667877371584">Standard properties</a>
            </b>
          </li>
        </ul>
        <p>
          Up: <a accesskey="u" href="Procedures.xhtml">Procedures</a></p>
        <p>
        Previous: <a accesskey="p" href="Application-and-Arguments-Lists.xhtml">Application and Arguments Lists</a></p>
        <p>
        Next: <a accesskey="n" href="Generic-procedures.xhtml">Generic (dynamically overloaded) procedures</a></p>
      </div>
    </footer>
  </body>
</html>
